package com.yao.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yao.mall.common.ErrorCode;
import com.yao.mall.common.IdRequest;
import com.yao.mall.common.PageRequest;
import com.yao.mall.common.Result;
import com.yao.mall.config.BusinessException;
import com.yao.mall.model.dto.UserRequest;
import com.yao.mall.model.dto.UserSearchRequest;
import com.yao.mall.model.entity.User;
import com.yao.mall.model.vo.UserVO;
import com.yao.mall.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author jiahua.yao
 * @version 1.0
 * @description 用户api
 * @date 2024/2/29 14:08
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Tag(name = "用户信息相关接口")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/getUserList")
    @Operation(summary = "管理员获取用户信息")
    public Result<Page<UserVO>> getUserList(@RequestBody UserSearchRequest userSearchRequest) {
        Page<UserVO> userVOList = userService.getUserList(userSearchRequest);
        return Result.success(userVOList);
    }

    @PostMapping("/ban")
    @Operation(summary = "封禁用户")
    public Result<Boolean> beanUser(@RequestBody IdRequest idRequest) {
        Boolean banBoolean = userService.toBanUser(idRequest.getId());
        return Result.success(banBoolean);
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result<UserVO> login(@RequestBody UserRequest userRequest){
        /*登录判断规则
        * 1、传输空数据
        * 2、账号密码登录
        * 3、手机验证码登录
        * 4、邮箱验证登录
        * */
        if (userRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        UserVO userVO = userService.userLogin(userRequest);
        return Result.success(userVO);
    }

    @PostMapping("/sendLoginCode")
    @Operation(summary = "发送登录验证码")
    //value = "发送验证码", notes = "userRequest中的登录类型、邮箱或电话不能为null"
    public Result<Boolean> sendLoginCode(@RequestBody UserRequest userRequest){
        if (userRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Boolean varCode = userService.sendLoginCode(userRequest);
        return Result.success(varCode);
    }

    @PostMapping("/sendPhoneCode")
    @Operation(summary = "发送手机验证码")
    public Result<Boolean> sendPhoneCode(@RequestBody UserRequest userRequest){
        if (userRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Boolean aBoolean = userService.sendPhoneCode(userRequest);
        return Result.success(aBoolean);
    }

    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public Result<String> userRegister(@RequestBody UserRequest userRequest){
        if (userRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        String strAccount = userService.userRegister(userRequest);
        return Result.success(strAccount);
    }

    @PostMapping("/updateBasic")
    @Operation(summary = "用户基础信息更新")
    public Result<UserVO> userUpdateBase(@RequestBody UserRequest userRequest){
        if (userRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        UserVO userVO = userService.userUpdateBasic(userRequest);
        return Result.success(userVO);
    }

    @PostMapping("/updatePic")
    @Operation(summary = "更新用户头像")
    public Result<String> userUpdatePic(@RequestParam("file") MultipartFile file){
        if (file.isEmpty()) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        String picStr = userService.userUpdatePic(file);
        return Result.success(picStr);
    }

    @PostMapping("/logout")
    @Operation(summary = "用户注销")
    public Result<Boolean> userLogout(HttpServletRequest request) {
        if (request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = userService.userLogout(request);
        return Result.success(result);
    }

}
